// ==UserScript==
// @name           autoCopy.uc.js
// @namespace      ithinc#mozine.cn
// @description    自动复制按钮版（切换图标蓝色复制，绿色复制并粘贴到搜索栏，红色禁用）
// @include        main
// @compatibility  Firefox 3.0.x
// @author         ithinc
// @updator       iwo
// @update        2013-01-21 21:30 respected ctrl or alt Key
// @update        2013-01-06 10:00 excluding input area
// @version        LastMod 2009/3/1 22:30 Initial release
// @Note           https://g.mozest.com/redirect.php?goto=findpost&pid=299093&ptid=42980
// ==/UserScript==

/* :::: AutoCopy with AutoPaste to search bar :::: */

(function() {
  var lastSelection = "";
  var autocopyImages = ["",
  "",
  ""];
  var autocopyTooltips = ["自动复制禁用", "自动复制\n不开启自动粘贴", "自动复制\n开启自动粘贴"];

  var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
  if(!prefs.getPrefType("userChrome.autocopy.autocopyState")) prefs.setIntPref("userChrome.autocopy.autocopyState", 2);

  function autocopyStart(e) {
    lastSelection = getBrowserSelection();
  }

  function autocopyStop(e) {
    var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
    var autocopyState = prefs.getIntPref("userChrome.autocopy.autocopyState");
    var selection = getBrowserSelection();
//增加判断是否在输入框或按下功能键	
	var exceptTarget = (e.target.nodeName == "TEXTAREA" || e.target.type == "textarea" || e.target.type == "text" || e.target.type == "password" || e.target.type == "email");
	var exceptoriginalTarget = (!e.originalTarget.ownerDocument || e.originalTarget.ownerDocument.designMode == "off" || e.originalTarget.ownerDocument.designMode == "undefined");
	var exceptAlternativeKey = (e.ctrlKey || e.altKey);
	var except = (exceptTarget && exceptoriginalTarget && !exceptAlternativeKey);//

    if(autocopyState>0 && selection && selection!=lastSelection && !except) {//
      goDoCommand('cmd_copy');

      if(autocopyState>1) {
        var searchbar = document.getElementById('searchbar');
        searchbar.removeAttribute("empty");
        searchbar.value = selection;

        var evt = document.createEvent("Events");
        evt.initEvent("oninput", true, true);
        searchbar.dispatchEvent(evt);
      }
    }
  }

  gBrowser.mPanelContainer.addEventListener("mousedown", autocopyStart, false);
  gBrowser.mPanelContainer.addEventListener("mouseup", autocopyStop, false);

  var statusbarpanel = document.getElementById("urlbar-icons").appendChild(document.createElement("statusbarpanel"));;
  statusbarpanel.setAttribute("id", "autocopy-statusbarpanel");
  statusbarpanel.setAttribute("class", "statusbarpanel-iconic");
  // statusbarpanel.insertBefore(newItem refChild);//
  statusbarpanel.setAttribute("onclick", '\
    if(event.button==0) {\
      var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);\
      var autocopyState = prefs.getIntPref("userChrome.autocopy.autocopyState");\
      prefs.setIntPref("userChrome.autocopy.autocopyState", (autocopyState+1)%3);\
    }\
  ');

  function refreshStatus() {
    var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
    var autocopyState = prefs.getIntPref("userChrome.autocopy.autocopyState");
    var statusbarpanel = document.getElementById("autocopy-statusbarpanel");

    statusbarpanel.setAttribute("src", autocopyImages[autocopyState%3]);
    statusbarpanel.tooltipText = autocopyTooltips[autocopyState%3];
  }
  refreshStatus();

  var observer = {
    observe:function(subject, topic, prefName) {refreshStatus();}
  };
  prefs.QueryInterface(Ci.nsIPrefBranchInternal).addObserver("userChrome.autocopy.autocopyState", observer, false);
})();